#include <stdio.h>
#include <stdlib.h>
#include "AnimationManager.h"

Vector2D * AnimationManager::GetVertices()
{
	Vector2D * vertices = (Vector2D *)malloc(sizeof(Vector2D) * 4);
	
	int width = frameSequence[currentFrame] % widthFrames;
	int height = frameSequence[currentFrame] / widthFrames;

	vertices[0] = Vector2D((float)width/(float)widthFrames, ((float)height + 1)/(float)heightFrames);
	vertices[1] = Vector2D(((float)width + 1)/(float)widthFrames, ((float)height + 1)/(float)heightFrames);
	vertices[2] = Vector2D(((float)width + 1)/(float)widthFrames, ((float)height)/(float)heightFrames);
	vertices[3] = Vector2D(((float)width)/(float)widthFrames, ((float)height)/(float)heightFrames);

	return vertices;
}
AnimationManager::AnimationManager(double timeBetweenFrames, int * frameSequence, int numFrames, int widthFrames, int heightFrames)
{
	this->timeBetweenFrames = timeBetweenFrames;
	this->frameSequence = frameSequence;
	this->numFrames = numFrames;
	this->widthFrames = widthFrames;
	this->heightFrames = heightFrames;
	this->currentFrame = 0;
	this->timeSoFar = 0.0;
}
AnimationManager::AnimationManager()
{
	this->timeBetweenFrames = 0.001;
	this->frameSequence = NULL;
	this->numFrames = 0;
	this->widthFrames = 0;
	this->heightFrames = 0;
	this->currentFrame = 0;
	this->timeSoFar = 0.0;
}
AnimationManager::~AnimationManager()
{
	if(frameSequence)
		delete(frameSequence);
	frameSequence = NULL;
}
void AnimationManager::SetTimeBetweenFrames(double time)
{
	timeBetweenFrames = time;
}
void AnimationManager::AddElapsedTime(double elapsedTime)
{
	timeSoFar += elapsedTime;
	if(timeSoFar > timeBetweenFrames && timeBetweenFrames > 0.0)
	{
		timeSoFar -= timeBetweenFrames;
		currentFrame = (currentFrame+1)%numFrames;
	}
}
